package jdbc.dao.impl;

import jdbc.dao.UserDao;
import jdbc.entity.User;
import jdbc.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Author：汤小洋
 * Date：2022-11-23 15:53
 * Description：<描述>
 */
@Repository
public class UserDaoImpl implements UserDao {

    @Autowired
    private JdbcTemplate jt;


    @Override
    public void insert(User user) {
        String sql = "insert into t_user (username,password) values (?,?)";
        jt.update(sql, user.getUsername(), user.getPassword());
    }

    @Override
    public User selectByUsernameAndPassword(String username, String password) {
        String sql = "select id,username,password from t_user where username=? and password=?";
        List<User> list = jt.query(sql, new UserMapper(), username, password);
        return list.isEmpty() ? null : list.get(0);
    }
}
